﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace SpaceGame
{
    class Player
    {
        private Ship ship;
        private bool active;

        public Player(ContentManager content)
        {
            ship = new Frigate(content, 0f, 1f, Vector2.Zero);
            active = true;
        }

        public Ship Ship
        {
            get { return Ship; }
            set { ship = value; }
        }

        public void Draw(SpriteBatch theSpriteBatch)
        {
            ship.Draw(theSpriteBatch);
        }

        public void Update(GameTime theGameTime)
        {
            KeyboardState currentKeyState = Keyboard.GetState();
            UpdateMovement(currentKeyState);
            ship.Update(theGameTime);
        }

        public void UpdateMovement(KeyboardState cks)
        {
            if (cks.IsKeyDown(Keys.A) == true)
            {
                Vector2 speedDelta = new Vector2((float)Math.Cos(ship.Rotation) * 5, (float)Math.Sin(ship.Rotation) * 5);
                ship.Velocity -= speedDelta;
                //move left relative
            }
            if (cks.IsKeyDown(Keys.D) == true)
            {
                Vector2 speedDelta = new Vector2((float)Math.Cos(ship.Rotation) * 5, (float)Math.Sin(ship.Rotation) * 5);
                ship.Velocity += speedDelta;
                //move right relative
            }
            if (cks.IsKeyDown(Keys.W) == true)
            {
                Vector2 speedDelta = new Vector2((float)Math.Cos(ship.Rotation - MathHelper.Pi / 2) * 5, (float)Math.Sin(ship.Rotation - MathHelper.Pi / 2) * 5);
                ship.Velocity += speedDelta;
                //move forward relative
            }
            if (cks.IsKeyDown(Keys.S) == true)
            {
                Vector2 speedDelta = new Vector2((float)Math.Cos(ship.Rotation - MathHelper.Pi / 2) * 5, (float)Math.Sin(ship.Rotation - MathHelper.Pi / 2) * 5);
                ship.Velocity -= speedDelta;
                //move backward relative
            }
            if (cks.IsKeyDown(Keys.Q) == true)
            {
                ship.RotationSpeed -= 0.002f;
            }
            if (cks.IsKeyDown(Keys.E) == true)
            {
                ship.RotationSpeed += 0.002f;
            }
            if (cks.IsKeyDown(Keys.R) == true)
            {
                ship.Turrets[0].RotationSpeed -= 0.01f;
                ship.Turrets[1].RotationSpeed -= 0.01f;
                Console.WriteLine(ship.Turrets[1].RotationSpeed);
            }
            if (cks.IsKeyDown(Keys.F) == true)
            {
                ship.Turrets[0].RotationSpeed += 0.01f;
                ship.Turrets[1].RotationSpeed += 0.01f;
                Console.WriteLine(ship.Turrets[1].RotationSpeed);
            }

            if (Math.Abs(ship.Velocity.X) > Math.Abs(ship.MaxSpeed))
            {
                ship.Velocity = new Vector2(ship.MaxSpeed * Math.Sign(ship.Velocity.X), ship.Velocity.Y);
            }
            if (Math.Abs(ship.Velocity.Y) > Math.Abs(ship.MaxSpeed))
            {
                ship.Velocity = new Vector2(ship.Velocity.X, ship.MaxSpeed * Math.Sign(ship.Velocity.Y));
            }
            if (Math.Abs(ship.RotationSpeed) > Math.Abs(ship.MaxRotationSpeed))
            {
                ship.RotationSpeed = Math.Sign(ship.RotationSpeed) * ship.MaxRotationSpeed;
            }

            ship.Rotation += ship.RotationSpeed;

            /*
            MouseState cms, pms;
            cms = Mouse.GetState();
            Vector2 cursor = new Vector2(cms.X, cms.Y);
            foreach (Turret t in ship.Turrets)
            {
                t.Rotation = (float)Math.Atan(((cms.Y - t.Origin.Y) / (cms.X - t.Origin.X)));
            }
            */
        }
    }
}
